package com.evildog.workspace.common.result;


import com.evildog.workspace.common.exception.CommonException;
import com.evildog.workspace.common.exception.ServerErrorException;
import com.evildog.workspace.common.enums.CommonCustomExceptionDesc;
import com.evildog.workspace.common.intefaces.ICustomExceptionDesc;

public class ResultGenerator {

    /**
     * success
     */

    public static <T> Result<T> success() {
        return success(null);
    }

    public static <T> Result<T> success(T data) {
        return new Result<>("200", "request ok!", data, Boolean.TRUE);
    }


    /**
     * error
     */

    public static <T> Result<T> error() {
        return error(new ServerErrorException(CommonCustomExceptionDesc.SERVER_ERROR));
    }

    public static <T> Result<T> error(CommonException e) {
        return error(e.getCode(), e.getErrorMessage());
    }

    public static <T> Result<T> error(ICustomExceptionDesc desc) {
        return error(desc.getCode(), desc.getMessage());
    }


    public static <T> Result<T> error(String code, String message) {
        return new Result<>(code, message, null, Boolean.FALSE);
    }


    public static Result<Boolean> status(boolean isSuccess) {
        return isSuccess ? success() : error();
    }

}
